/** \file
* WIN32 implementation of the debug console class.
* This file is part of Chorus, available at
* http://code.google.com/p/chorus-twitter/ . This file and all associated files
* and derivative works are bound by the terms of the LGPL 3.0 license,
* available at http://www.gnu.org/licenses/lgpl-3.0-standalone.html .
* \par Subversion URL:
* \$URL: http://chorus-twitter.googlecode.com/svn/branches/cpp/src/system/win32/DebugConsole.cpp $
* \version \$Rev: 153 $
* \date    \$Date: 2010-04-06 07:43:42 +0000 (Tue, 06 Apr 2010) $
*/

#define NEEDS_WIN32
#include "config.h"

#ifdef USING_DEBUGCONSOLE

#include "system/DebugConsole.hpp"

using namespace std;

void DebugConsole::PrintWin32::print(std::ostringstream &buffer)
{
	// Get the last system-level error.
	const DWORD error = GetLastError();
	buffer << "    " << error << ": ";

	// Format an error string describing the error.
	char *str;
	if (FormatMessage(
		FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
		FORMAT_MESSAGE_IGNORE_INSERTS,
		0, error, 0, (LPSTR)&str, 0, 0))
	{
		buffer << str << '\n';
		if (LocalFree(str))
			buffer << "\n    Failed to free error string\n";
	}
	else
		buffer << "Unknown error\n";

	// Clear the error, just in case.
	SetLastError(ERROR_SUCCESS);
}

#endif // #ifdef USING_DEBUGCONSOLE
